﻿function getSpecLook() {
    var oprno = $('#oprno').val();
    var isCurrentFactory = '0';
    if ($('#hidCurrentPage').val() >= $('#hidPageCount').val()) {
        $('#hidCurrentPage').val('1');
    } else {
        $('#hidCurrentPage').val(Number($('#hidCurrentPage').val()) + 1);
    }
    if ($("#checkIsCurrentFactory").get(0).checked) {
        isCurrentFactory = '1';
    }
    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        url: "uMESPartReportingHandler.ashx",
        data: {
            "Type": "GetSpec",
            "OprNo": oprno,
            "CurrentPageIndex": Number($('#hidCurrentPage').val()),
            "FactoryId": getUrlParam('factoryid'),
            "IsCurrentFactory": isCurrentFactory
        },
        success: function (result) {
            $('.tabLook').empty();
            $('.tabLook').html('<tr style=\"height:30px;\"><th style="width:50px;">序号</th>' +
                    '<th style="width:150px;">工作令号</th>' +
                    '<th style="width:110px; display:none;">图号</th>' +
                    '<th style="width:150px;">名称</th>' +
                    '<th style="width:220px;">批次号</th>' +
                    '<th style="width:150px;">计划结束时间</th>' +
                    '<th style="width:55px;">数量</th><th></th></tr>');
            if (result.code != 0) {
                alert(result.msg);
                return;
            }
            $('#hidPageCount').val(result.pageCount);//总页数
            var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
            var maxSpecLen = 0;

            for (var i = 0; i < result.data.length; i++) {
                if (maxSpecLen < result.data[i].specs.length) {
                    maxSpecLen = result.data[i].specs.length;
                }
            }

            $('#divtit').css('width', w + 'px');
            $('#ruler').css('width', 885 + maxSpecLen * 160 + 'px');
            $('#divLook').css('width', 885 + maxSpecLen * 160 + 'px').css('min-width', w + 'px');
            var bgcolors = ["#ffc0cb", "Yellow", "#FFD306", "YellowGreen", "#00CCFF", "Green"];
            for (var i = 0; i < result.data.length; i++) {
                var strtr = '<tr><td style="width:50px; min-width:50px; height:30px;">' + result.data[i].rowno + '</td><td style="width:150px; min-width:150px; height:30px;">' + result.data[i].oprno + '</td><td style="width:110px; min-width:110px; height:30px; display:none;">' + result.data[i].productname + '</td>';
                strtr += '<td style="width:150px; min-width:150px; height:30px;">' + result.data[i].description + '</td><td style="width:220px; min-width:220px; height:30px;">' + result.data[i].containername + '</td>';
                //var sDate = new Date(result.data[i].plannedstartdate);
                var eDate = new Date(result.data[i].plannedcompletiondate);
                if (eDate < new Date()) {
                    strtr += '<td style="width:150px; min-width:150px; height:30px;color:#ff0000;">' + result.data[i].plannedcompletiondate + '</td>';
                } else {
                    strtr += '<td style="width:150px; min-width:150px; height:30px;">' + result.data[i].plannedcompletiondate + '</td>';
                }
                strtr += '<td style="width:55px; min-width:55px; height:30px;">' + result.data[i].conqty + '</td><td style=\" height:30px;\"><table class=\"tabspec\"><tr>';
                for (var j = 0; j < result.data[i].specs.length; j++) {
                    strtr += '<td style="width:160px; min-width:160px; height:30px;">' + result.data[i].specs[j].specname.replace(result.data[i].productname + "-", "") + '</td>';
                }
                strtr += '</tr></table></td></tr>';
                $('.tabLook').html($('.tabLook').html() + strtr);
                $('.tabLook>tbody>tr:eq(' + ($('.tabLook>tbody>tr').length - 1) + ') td:last-child').find('.tabspec tr').css('height', $('.tabspec').get($('.tabspec').length - 1).parentNode.offsetHeight + 'px');
                strtr = '<tr><td style=\"height:30px;\"></td><td style=\"height:30px;\"></td><td style=\"height:30px;display:none;\"></td><td style=\"height:30px;\"></td><td style=\"height:30px;\">' + (result.data[i].status == '正常' ? '' : result.data[i].status) + '</td>';
                if (eDate < new Date()) {
                    strtr += '<td style=\"height:30px; color:#ff0000;\">延期</td>';
                } else {
                    strtr += '<td style=\"height:30px;\"></td>';
                }
                strtr += '<td style=\"height:30px;\"></td><td style=\"height:30px;\"><table class=\"tabspec\"><tr>';
                for (var j = 0; j < result.data[i].specs.length; j++) {
                    if (typeof (bgcolors[parseInt(result.data[i].specs[j].specstatus)]) == "undefined") {
                        strtr += '<td style=\"height:30px;\">';
                    } else {
                        strtr += '<td style=\"height:30px; background-color:' + bgcolors[parseInt(result.data[i].specs[j].specstatus)] + '\">';
                    }
                    if (parseInt(result.data[i].specs[j].isquality) === 1) {
                        strtr += '<span style=\"color:#ff0000;\">▲</span>';
                    }
                    if (parseInt(result.data[i].specs[j].synerspec) === 1) {
                        strtr += '外协';
                    }
                    strtr += '</td>';
                }
                strtr += '</tr></table></td></tr>';
                $('.tabLook').html($('.tabLook').html() + strtr);
                $('.tabLook>tbody>tr:eq(' + ($('.tabLook>tbody>tr').length - 1) + ') td:last-child').find('.tabspec tr').css('height', $('.tabspec').get($('.tabspec').length - 1).parentNode.offsetHeight + 'px');
            }
            $('.tabLook tr td:last-child').css('border', 'none');
            $('.tabspec tr td').css('border-top', 'none').css('border-bottom', '1px solid #41497c').css('border-left', 'none').css('border-right', '1px solid #41497c');
            for (var i = 0; i < $('.tabLook>tbody>tr').length - 1; i++) {
                if ($('.tabLook>tbody>tr:eq(' + i + ')>td:first-child').text() == '') {
                    $('.tabLook>tbody>tr:eq(' + i + ')>td:last-child').find('.tabspec tr td').css('border-bottom', '1px solid #41497c');
                } else {
                    if (Number($('.tabLook>tbody>tr:eq(' + i + ')>td:first-child').text()) % 10 != 1) {
                        $('.tabLook>tbody>tr:eq(' + i + ')>td:last-child').find('.tabspec tr td').css('border-top', '1px solid #41497c');
                    } else {
                        $('.tabLook>tbody>tr:eq(' + i + ')>td:last-child').find('.tabspec tr td').css('border-top', 'none');
                    }
                }
            }
        },
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
};
//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
};